<?php

declare(strict_types=1);

namespace app\core;

use Swoole\Database\RedisPool;
use Swoole\Database\RedisConfig;
use Exception;
class RedisDB
{
    public $pool = null;
    private static $instance;
    public function __construct()
    {
        try {


            if (empty($this->pool)) {
                $this->pool = new RedisPool((new RedisConfig)
                ->withHost('127.0.0.1')
                ->withPort(6379)
                ->withAuth('')
                ->withDbIndex(0)
                ->withTimeout(1)
            );
            }
        } catch (Exception $e) {
            $this->pool->put(null);
            throw new Exception($e);
          
        }
    }
    public static function getInstance()
    {

        if (empty(self::$instance)) {
            self::$instance = new static();
        }

        return self::$instance;
    }
    public function getConnection()
    {
        return $this->pool->get();
    }
    public function close($connection = null)
    {
        $this->pool->put($connection);
    }
}